package com.bainc.web.client;

import com.bainc.web.shared.LoginInfo;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.PasswordTextBox;

/*
 * Box where user enters his credentials for authentication.
 */
public class LoginBox extends Composite {

	private Label labelUsername;
	private Label lblNewLabel;
	private TextBox textBoxUsername;
	private PasswordTextBox textBoxPassword;

	public LoginBox() {
		VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);

		Label lblLoginToYour = new Label("Sign in to your account");
		lblLoginToYour.setStyleName("gwt-Label-Login-Header");
		verticalPanel.add(lblLoginToYour);

		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);

		labelUsername = new Label("Username");
		labelUsername.setStyleName("gwt-Label-Login");
		flexTable.setWidget(0, 0, labelUsername);

		textBoxUsername = new TextBox();
		flexTable.setWidget(0, 1, textBoxUsername);

		lblNewLabel = new Label("Password");
		lblNewLabel.setStyleName("gwt-Label-Login");
		flexTable.setWidget(1, 0, lblNewLabel);

		textBoxPassword = new PasswordTextBox();
		flexTable.setWidget(1, 1, textBoxPassword);

		CheckBox chckbxNewCheckBox = new CheckBox("Remember me on this computer");
		flexTable.setWidget(2, 1, chckbxNewCheckBox);

		Button btnNewButton = new Button("Sign In");
		btnNewButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				loadUserProfile();
			}
		});
		flexTable.setWidget(3, 1, btnNewButton);
	}
	
	private void loadUserProfile() {
		if (textBoxUsername.getText().length() == 0) {
			Window.alert("Username is empty."); 
		}
		// TODO(ktiwari): Add another service for login authentication.
		LoginInfo info = new LoginInfo();
		info.setLoggedIn(true);
		info.setLoginId(textBoxUsername.getText());
		// TODO(ktiwari): Load separate html page here.
		UserProfilePage.load(info);
	}
}
